﻿;function Filter(groupOP) {
    if (this.GroupOPs == undefined) {
        Filter.prototype.GroupOPs = ['AND', 'OR'];
    };
    if (this.RuleOPs == undefined) {
        Filter.prototype.RuleOPs = [{ name: "eq", description: "equal", operator: "=" },
            { name: "ne", description: "not equal", operator: "<>" },
            { name: "lt", description: "less", operator: "<" },
            { name: "le", description: "less or equal", operator: "<=" },
            { name: "gt", description: "greater", operator: ">" },
            { name: "ge", description: "greater or equal", operator: ">=" },
            { name: "bw", description: "begins with", operator: "LIKE" },
            { name: "bn", description: "does not begin with", operator: "NOT LIKE" },
            { name: "in", description: "in", operator: "IN" },
            { name: "ni", description: "not in", operator: "NOT IN" },
            { name: "ew", description: "ends with", operator: "LIKE" },
            { name: "en", description: "does not end with", operator: "NOT LIKE" },
            { name: "cn", description: "contains", operator: "LIKE" },
            { name: "nc", description: "does not contain", operator: "NOT LIKE" },
            { name: "nu", description: "is null", operator: "IS NULL" },
            { name: "nn", description: "is not null", operator: "IS NOT NULL" }];
    };
    if (this.IsValidGroupOP == undefined) {
        Filter.prototype.IsValidGroupOP = function (groupOP) {
            for (var i = 0; i < this.GroupOPs.length; i++) {
                if (this.GroupOPs[i] == groupOP) {
                    return true;
                }
            }
            return false;
        };
    };
    if (this.IsValidRuleOP == undefined) {
        Filter.prototype.IsValidRuleOP = function (ruleOP) {
            for (var i = 0; i < this.RuleOPs.length; i++) {
                if (this.RuleOPs[i].name == ruleOP) {
                    return true;
                }
            }
            return false;
        };
    };
    {
        this.groupOP = 'AND';
        this.rules = [];
        this.groups = [];
    }
    this.AddRuler = function (field, op, data) {
        if (!Filter.prototype.IsValidRuleOP(op)) {
            throw 'wrong ruleop';
        }
        this.rules[this.rules.length] = { "field": field, "op": op, "data": data };
        return this.rules[this.rules.length - 1];
    };
    this.AddGroup = function (groupOp) {
        if (!Filter.prototype.IsValidGroupOP(groupOp)) {
            throw 'wrong groupOp';
        }
        this.groups[this.groups.length] = { "groupOp": groupOp };
        return this.groups[this.groups.length - 1];
    };
    this.Clear = function () {
        this.groupOP = 'AND';
        this.rules = [];
        this.groups = [];
    };
    this.ToString = function () {
        return $.toJSON(this);
    };
    this.SetFilterGroupOP = function (groupOp) {
        if (Filter.prototype.IsValidGroupOP(groupOP)) {
            this.groupOP = groupOP;
        }
    };
    this.SetFilterGroupOP(groupOP);
}
function DataService(p) {
    this.filter = new Filter();
    this.pars = {
        url: null,
        qn: null,// your table or view name for get data ,is mandatory 
        idcol: 'id',//Primary key of table or view, default value is 'id' 
        coli: null,//how many columns will be retrive from table or view ,if not set will retrive all column from view
        page: 1,
        rows: 15,
        sidx: null,
        sord: null,
        searchField: null,
        searchString: null,
        searchOper: null,
        cached: true
    };
    {
        this.cacheData = new Hashtable();
    }
    if (p) {
        $.extend(this.pars, p);
    }
    this.GetQueryURL = function () {
        var url = this.pars.url;
        var tempint = 0;
        if (!url) {
            throw 'worng url in pars';
        }
        if (this.pars.qn) {
            url += '?qn=' + this.pars.qn;
        }
        else {
            throw 'worng qn in pars';
        }
        url += '&_search=true';
        if (this.pars.idcol) {
            url += '&idcol=' + this.pars.idcol;
        }
        tempint = parseInt(this.pars.coli);
        if (!isNaN(tempint)) {
            url += '&coli=' + tempint;
        }
        tempint = parseInt(this.pars.page);
        if (!isNaN(tempint)) {
            url += '&page=' + tempint;
        }
        tempint = parseInt(this.pars.rows);
        if (!isNaN(tempint)) {
            url += '&rows=' + tempint;
        }
        if (this.pars.sidx) {
            url += '&sidx=' + this.pars.sidx;
        }
        if (this.pars.sord) {
            url += '&sord=' + this.pars.sord;
        }
        if (this.pars.searchField) {
            url += '&searchField=' + this.pars.searchField;
            url += '&searchOper=' + this.pars.searchOper;
            url += '&searchString=' + this.pars.searchString;
        }
        return encodeURI(url);
    };
    this.GetData = function (callback) {
        var myurl = this.GetQueryURL();
        var filter = this.filter.ToString();
        if (this.pars.cached) {
            var data = this.cacheData.getItem(myurl + filter);
            if (data != null) {
                if (callback)
                    callback(data);
                return;
            }
        }     
        $.ajax({
            type: "POST",
            url: myurl,
            dataType: 'json',
            data: { "filters": filter },
            context:this,
            success: function (data) {
                if (this.pars.cached)
                    this.cacheData.setItem(myurl + filter, data);
                if (callback)
                    callback(data);
            }
        });
    }
};